<script>
jQuery(document).ready(function ()
{
    initSubmits();
});

/**
 * @param arrayOfParentDivs Array()
 * @param currentParentDiv jqueryObj
 */
function getRecursiveAjaxClasses(arrayOfParentDivs, currentParentDiv) {
    var parentDiv = currentParentDiv.parents('form, div:first[class=ajaxClass]');
    var parentId = parentDiv.attr('id');
    if (parentId != undefined) {
        arrayOfParentDivs.push(parentId);
//        console.log(arrayOfParentDivs);
        getRecursiveAjaxClasses(arrayOfParentDivs, parentDiv)
    } else {
    }
    return arrayOfParentDivs;
}

/**
 * @param currentParentDiv jqueryObj
 */
function setJqueryDelegateToSubmits(currentParentDiv) {
    var recursiveAjaxClassArray = getRecursiveAjaxClasses(new Array(), currentParentDiv);
    console.log(recursiveAjaxClassArray);
    var lengthOfRecursiveArray = recursiveAjaxClassArray.length;
    var navigationToDiv = undefined;
    if (0 < lengthOfRecursiveArray) {
        for (var i = lengthOfRecursiveArray - 1; i >= 0; i--) {
            if (i == lengthOfRecursiveArray - 1) {
                navigationToDiv = "#" + recursiveAjaxClassArray[i];
            } else if(i > 0) {
                navigationToDiv += " > #" + recursiveAjaxClassArray[i];
            } else {
                jQuery(navigationToDiv).undelegate(
                    "#" + recursiveAjaxClassArray[i],
                    "submit"
                );
                console.log(navigationToDiv);
                var formId = "#" + recursiveAjaxClassArray[i];
                jQuery(navigationToDiv).delegate(
                    formId,
                    "submit",
                    function(event) {
                        ajaxCall(event, navigationToDiv, formId);
                    }
                );
            }
        }
    }
    return navigationToDiv;
}

function initSubmits()
{
    var submits = jQuery("input[type=submit]")
    var forms = new Array();
    submits.each(function() {
        setJqueryDelegateToSubmits(jQuery(this));
    });
}

function ajaxCall(event, navigationToDiv, formId)
{
    event.preventDefault();
    var navigationToDiv = jQuery(navigationToDiv);
    navigationToDiv.addClass("update");
    var navigationToForm = navigationToDiv.find(formId);
    var params = navigationToForm.serializeArray();
    var urlParams = "script=true&" + jQuery.param(params);

    jQuery("#loading").show();

    jQuery.ajax({
        type: "POST",
        url: "index.php",
        data: urlParams,
        datatype: 'json',
        success: function(msg){
            var fetchedDivData = jQuery.parseJSON(msg);
            console.log(fetchedDivData.length);
            for (i=0; i < fetchedDivData.length; i++) {
                navigationToDiv = jQuery(fetchedDivData[i].navigationToDiv);
                console.log(navigationToDiv);
                if (navigationToDiv.attr('class').match('update')) {
                    navigationToDiv.removeClass('update');
                    navigationToDiv.find(fetchedDivData[i].divToReplace)
                        .html(fetchedDivData[i].content);
                }
            }
            console.log(fetchedDivData);
            jQuery("#loading").hide();
        },
        error: function() {
            alert('error occurred between ajax');
        }
    });
}

</script>
</body>
</html>